<template>
  <div class="app-container">
    <NormalForm pageTitle="参数设置" :operateForm="queryForm" :formList="formList" :enumOpt="enumOpt" @handleQuery="handleQuery" @handleReset="handleReset" />
    <div class="g-card" style="padding: 0 20px 20px 20px">
      <div class="right-btn" style="display: flex; justify-content: space-between">
        <div>
          <el-button type="info" plain icon="el-icon-download" @click="handleExport" v-hasPermi="['system:config:export']">导出</el-button>
        </div>
        <div style="margin-right: -20px">
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd" v-hasPermi="['system:config:add']">新增</el-button>
          <el-button icon="el-icon-delete" :disabled="currentSelectList.length == 0" @click="handleDelete" v-hasPermi="['system:config:remove']">删除</el-button>
          <el-button type="info" plain icon="el-icon-refresh" @click="handleClearCache" v-hasPermi="['system:config:remove']">清理缓存</el-button>
        </div>
      </div>
      <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="60" align="center" />
        <el-table-column label="参数主键" align="center" prop="configId" />
        <el-table-column label="参数名称" align="center" prop="configName" :show-overflow-tooltip="true" />
        <el-table-column label="参数键名" align="center" prop="configKey" :show-overflow-tooltip="true" />
        <el-table-column label="参数键值" align="center" prop="configValue" :show-overflow-tooltip="true" />
        <el-table-column label="系统内置" prop="configType" align="center" min-width="100">
          <template slot-scope="scope">
            <span>{{ configTypeOpt[scope.row.configType] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
        <el-table-column label="创建时间" align="center" prop="createTime" min-width="140">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="140">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['system:config:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['system:config:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <NormalPagination :currentPage="queryForm.pageNum" :pageSize="queryForm.pageSize" :total="total" @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange"></NormalPagination>
    </div>
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="updateVisible" width="500px" append-to-body :close-on-click-modal="false" :show-close="false">
      <el-form ref="updateForm" :model="updateForm" :rules="updateRules" label-width="80px">
        <el-form-item label="参数名称" prop="configName">
          <el-input v-model="updateForm.configName" placeholder="请输入参数名称" />
        </el-form-item>
        <el-form-item label="参数键名" prop="configKey">
          <el-input v-model="updateForm.configKey" placeholder="请输入参数键名" />
        </el-form-item>
        <el-form-item label="参数键值" prop="configValue">
          <el-input v-model="updateForm.configValue" placeholder="请输入参数键值" />
        </el-form-item>
        <el-form-item label="系统内置" prop="configType">
          <el-radio-group v-model="updateForm.configType">
            <el-radio v-for="item in enumOpt.configTypeList" :key="item.dictValue" :label="item.dictValue">
              {{ item.dictLabel }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="updateForm.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateSubmit">确 定</el-button>
        <el-button @click="updateCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConfig, getConfig, delConfig, addConfig, updateConfig, clearCache } from "@/api/system/config";
import { downLoadFile } from "@/utils/common";

export default {
  name: "Config",
  data() {
    return {
      queryForm: {
        configName: null,
        configKey: null,
        configType: null,
        dateRange: [],
        pageNum: 1,
        pageSize: 20,
      },
      formList: [
        {
          label: '参数名称',
          value: '',
          prop: 'configName',
          type: 'INPUT',
          emunName: ''
        },
        {
          label: '参数键名',
          value: '',
          prop: 'configKey',
          type: 'INPUT',
          emunName: ''
        },
        {
          label: '系统内置',
          value: '',
          prop: 'configType',
          type: 'SELECT',
          emunName: 'configTypeList'
        },
        {
          label: '创建',
          value: null,
          prop: 'dateRange',
          type: 'DATE_RANGE',
          emunName: '',
          formate: 'yyyy-MM-dd'
        },
      ],
      enumOpt: {
        configTypeList: [],
      },
      configTypeOpt: {},
      loading: true,
      total: 0,
      configList: [],
      currentSelectList: [],
      title: "",
      updateVisible: false,
      updateForm: {
        configName: null,
        configKey: null,
        configValue: null,
        configType: 'Y',
        remark: null,
      },
      updateRules: {
        configName: [{ required: true, message: "参数名称不能为空", trigger: "blur" }],
        configKey: [{ required: true, message: "参数键名不能为空", trigger: "blur" }],
        configValue: [{ required: true, message: "参数键值不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getDicts("sys_yes_no").then((res) => {
      if (res.code == 200) {
        this.enumOpt.configTypeList = res.data;
        res.data.forEach(item => {
          this.configTypeOpt[item.dictValue] = item.dictLabel;
        })
      }
    });
    this.handleQuery();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryForm.pageNum = 1;
      this.getTableListFn();
    },
    /** 重置按钮操作 */
    handleReset() {
      this.queryForm = {
        configName: null,
        configKey: null,
        configType: null,
        dateRange: [],
        pageNum: 1,
        pageSize: 20,
      };
      this.getTableListFn();
    },
    handleSizeChange(val) {
      this.queryForm.pageNum = 1
      this.queryForm.pageSize = val
      this.getTableListFn()
    },
    handleCurrentChange(val) {
      this.queryForm.pageNum = val
      this.getTableListFn()
    },
    /** 查询参数列表 */
    getTableListFn() {
      this.loading = true;
      const params = {
        configName: this.queryForm.configName,
        configKey: this.queryForm.configKey,
        configType: this.queryForm.configType,
        beginTime: this.queryForm.dateRange?.[0],
        endTime: this.queryForm.dateRange?.[1],
        pageNum: this.queryForm.pageNum,
        pageSize:  this.queryForm.pageSize,
      }
      listConfig(params).then((res) => {
        this.configList = res.rows || [];
        this.total = res.total || 0;
        this.loading = false;
      });
    },
    // 取消按钮
    updateCancel() {
      this.updateVisible = false;
      this.$refs.updateForm.resetFields();
      this.updateForm = {
        configName: null,
        configKey: null,
        configValue: null,
        configType: 'Y',
        remark: null,
      };
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.updateVisible = true;
      this.title = "添加参数";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.currentSelectList = selection.map((item) => item.configId);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const configId = row.configId || this.currentSelectList;
      getConfig(configId).then((response) => {
        this.updateForm = response.data;
        this.updateVisible = true;
        this.title = "修改参数";
      });
    },
    /** 提交按钮 */
    updateSubmit() {
      this.$refs.updateForm.validate((valid) => {
        if (valid) {
          if (this.updateForm.configId) {
            updateConfig(this.updateForm).then((res) => {
              if (res.code === 200) {
                this.$message.success("修改成功");
                this.updateCancel();
                this.getTableListFn();
              }
            });
          } else {
            addConfig(this.updateForm).then((res) => {
              if (res.code === 200) {
                this.$message.success("新增成功");
                this.updateCancel();
                this.getTableListFn();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configIds = row.configId || this.currentSelectList;
      this.$confirm('是否确认删除参数编号为"' + configIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(function () {
        return delConfig(configIds);
      }).then(() => {
        this.getTableListFn();
        this.$message.success("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const params = {
        configName: this.queryForm.configName,
        configKey: this.queryForm.configKey,
        configType: this.queryForm.configType,
        beginTime: this.queryForm.dateRange?.[0],
        endTime: this.queryForm.dateRange?.[1],
      }
      downLoadFile('/system/config/export', params, '参数列表.xlsx')
    },
    /** 清理缓存按钮操作 */
    handleClearCache() {
      clearCache().then((response) => {
        if (response.code === 200) {
          this.$message.success("清理成功");
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../../styles/base.scss";
</style>